'use strict';
app.controller('PolicyTypeCtrl', ['$scope', '$modal', '$rootScope', '$state', '$filter', 'toaster', 'commonService', 'userService', function ($scope, $modal, $rootScope, $state, $filter, toaster, commonService, userService) {
    $scope.page_code = "policyType";
    $scope.matchString = "";

    function getTbDefinitionsFromServer() {
        return {
            "serverSide": true,
            "ajax": {
                url: "policyType/plist", data: function (d) {
                    d.matchString = $scope.matchString;
                    for (var key in d) {
                        if (key.indexOf("columns") == 0) {
                            delete d[key];
                        }
                    }
                }
            },
            aoColumns: [
                {mData: 'id'},
                {mData: 'name'},
                {mData: 'orderId'},
                {mData: 'createDate'}
                ],
            aoColumnDefs: [{
                aTargets: [0], mRender: function (data, type, full) {
                    var editId = "policyType" + data;
                    return '<label class="i-checks m-b-none"><input id = "' + editId + '" class="selectListCmPara" type="checkbox"><i></i></label>';
                }, bSortable: false
            },commonService.tableRowAllowNullNoSort(1),
                commonService.tableRowAllowNullNoSort(2),
                {
                    aTargets : [ 3 ],
                    mRender : function(data, type,
                                       full) {
                        if(data){
                            var date = $filter('date')(new Date(data),'yyyy-MM-dd');
                            return date;
                        }
                        return "";
                    },
                    bSortable:false
                }],
            fnDrawCallback: function (oSettings, json) {
                $(".selectListCmPara").on('click', function () {
                    $(".selectListCmPara").not(this).attr("checked", false);
                });
            }
        };
    };$scope.tbOptions = getTbDefinitionsFromServer();
    $scope.add = function () {
        var param = {type: 'create'};
        commonService.openEditDialog(param, 'edit', 'PolicyTypeEditCtrl');
    };
    $scope.edit = function () {
        var info = commonService.getSelectData("policyTypeTable", "policyType", "id");
        if (info != null) {
            var param = {type: 'edit', info: info};
            commonService.openEditDialog(param, 'edit', 'PolicyTypeEditCtrl');
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.delete = function () {
        var info = commonService.getSelectData("policyTypeTable", "policyType", "id");
        if (info != null) {
            BootstrapDialog.confirm("确认要删除：" + info.name, function (result) {
                if (result) {
                    commonService.httpJson("policyType/ditem", {id: info.id}).success(function (data, status) {
                        if (data &&  data['code'] == 0) {
                            toaster.pop('info', '提示', data['message']);
                            $scope.refresh();
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
                }
            });
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.refresh = function () {
        var table = $('#policyTypeTable').DataTable();
        table.ajax.reload();
    };
    $scope.search = function () {
        $scope.refresh();
    };
    $scope.$on('policyType.update', function (event) {
        $scope.refresh();
    });
}]);
app.controller('PolicyTypeEditCtrl', ['$scope', '$rootScope', '$modalInstance', 'param', 'toaster', 'commonService', 'userService', function ($scope, $rootScope, $modalInstance, param, toaster, commonService, userService) {
    $scope.policyType = {};

    if (param.type == "edit") {
        $scope.policyType = param.info;
    }

    function checkSave() {
        var result = true;
        if (!$scope.policyType.name) {
            commonService.toasterError("名称不能为空");
            result = false;
        }
        return result;
    }

    $scope.submit = function () {
        if (!checkSave()) {
            return;
        }
        if (param.type == "edit") {
            $scope.policyType.updateUser = userService.getId();
        } else {
            $scope.policyType.createUser = userService.getId();
        }
        commonService.httpJson("policyType/save", $scope.policyType).success(function (data) {
            if (data && data['code'] == 0) {
                commonService.editData("policyType.update", param.type);
                toaster.pop("info", "信息", data['message']);
                $modalInstance.close();
            } else {
                commonService.toasterError(data['message']);
            }
        });
    }
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);